package zhongbo.java.air.airmonitor.task;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import zhongbo.java.air.airmonitor.mapper.FlightActivityDailyMapper;
import zhongbo.java.air.airmonitor.model.FlightActivityDaily;
import zhongbo.java.air.airmonitor.service.IFlightActivityDailyService;


import java.util.Date;
import java.util.List;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月08日 星期二 04:12:59
 */
@Component
@Slf4j
public class AircraftActivityTask {

    @Autowired
    private FlightActivityDailyMapper flightActivityDailyMapper;

    @Autowired
    private IFlightActivityDailyService<FlightActivityDaily> flightActivityDailyService;

    // 每10分钟执行（注意配置时区）
    @Scheduled(
            // 开发中可设置短一点, 这里设置5分钟
            fixedDelay = 1 * 60 * 1000
            // 启动后等多少毫秒, 执行第一次定时任务, 这里设置5秒
            , initialDelay = 5000
    )
    public void updateDailyActivity() {
        log.info("---- 执行定时任务 -----");
        doJob();
    }

    private void doJob() {
        String today = DateUtil.formatDate(new Date());
        // 1. 从轨迹表查询当天出现过的ICAO码（需实现轨迹表查询逻辑）
        List<FlightActivityDaily> activeICaos = flightActivityDailyMapper.selectDailyStats(today);
        log.info("查询结果: {}条", activeICaos.size());
        log.info("查询结果: {}", activeICaos.get(0));
        // 2. 批量插入或更新
        // todo 周二任务: 存入数据库. 存入之前先icao+date两个字段, 判断数据库是否已有数据, 有的话则是更新
        for (FlightActivityDaily activeICao : activeICaos){
            FlightActivityDaily flightActivityDaily = flightActivityDailyService.queryByIcaoCodeAndDate(activeICao.getIcaoCode(), today);
            if (flightActivityDaily == null){
                FlightActivityDaily queryByIcao = flightActivityDailyService.queryByIcao(activeICao.getIcaoCode());
                if (queryByIcao == null){
                    activeICao.setAirline("UNKNOWN");
                    activeICao.setRegistration("UNKNOWN");
                    activeICao.setAircraftType("UNKNOWN");
                }else {
                    activeICao.setAirline(queryByIcao.getAirline());
                    activeICao.setRegistration(queryByIcao.getRegistration());
                    activeICao.setAircraftType(queryByIcao.getAircraftType());
                }
                activeICao.setDate(DateUtil.parse(today));
                activeICao.setCreatedTime(new Date());
                activeICao.setUpdatedTime(new Date());
                flightActivityDailyService.insert(activeICao);
            }
            if (flightActivityDaily != null){
                activeICao.setId(flightActivityDaily.getId());
                activeICao.setRegistration(flightActivityDaily.getRegistration());
                activeICao.setAirline(flightActivityDaily.getAirline());
                activeICao.setAircraftType(flightActivityDaily.getAircraftType());
                activeICao.setCreatedTime(flightActivityDaily.getCreatedTime());
                activeICao.setUpdatedTime(new Date());
                activeICao.setDate(flightActivityDaily.getDate());
                flightActivityDailyService.update(activeICao);
            }
        }
    }

}
